import { getInventoryList, addInventory, updateInventory, deleteInventory, getInventoryInList, addInventoryIn, getInventoryOutList, addInventoryOut } from '@/api/inventory'

const state = {
  list: [],
  total: 0,
  loading: false,
  inList: [],
  inTotal: 0,
  outList: [],
  outTotal: 0
}

const mutations = {
  SET_LIST: (state, list) => {
    state.list = list
  },
  SET_TOTAL: (state, total) => {
    state.total = total
  },
  SET_LOADING: (state, loading) => {
    state.loading = loading
  },
  SET_IN_LIST: (state, list) => {
    state.inList = list
  },
  SET_IN_TOTAL: (state, total) => {
    state.inTotal = total
  },
  SET_OUT_LIST: (state, list) => {
    state.outList = list
  },
  SET_OUT_TOTAL: (state, total) => {
    state.outTotal = total
  }
}

const actions = {
  // Get inventory list
  getInventoryList({ commit }, params) {
    commit('SET_LOADING', true)
    return new Promise((resolve, reject) => {
      getInventoryList(params)
        .then(response => {
          const { data } = response
          commit('SET_LIST', data.items)
          commit('SET_TOTAL', data.total)
          commit('SET_LOADING', false)
          resolve(data)
        })
        .catch(error => {
          commit('SET_LOADING', false)
          reject(error)
        })
    })
  },

  // Get inventory in list
  getInventoryInList({ commit }, params) {
    return new Promise((resolve, reject) => {
      getInventoryInList(params)
        .then(response => {
          const { data } = response
          commit('SET_IN_LIST', data.items)
          commit('SET_IN_TOTAL', data.total)
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // Add inventory in
  addInventoryIn(_, data) {
    return new Promise((resolve, reject) => {
      addInventoryIn(data)
        .then(response => {
          const { data } = response
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // Get inventory out list
  getInventoryOutList({ commit }, params) {
    return new Promise((resolve, reject) => {
      getInventoryOutList(params)
        .then(response => {
          const { data } = response
          commit('SET_OUT_LIST', data.items)
          commit('SET_OUT_TOTAL', data.total)
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // Add inventory out
  addInventoryOut(_, data) {
    return new Promise((resolve, reject) => {
      addInventoryOut(data)
        .then(response => {
          const { data } = response
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // Add inventory
  addInventory(_, inventoryData) {
    return new Promise((resolve, reject) => {
      addInventory(inventoryData)
        .then(response => {
          const { data } = response
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // Update inventory
  updateInventory(_, { id, data }) {
    return new Promise((resolve, reject) => {
      updateInventory(id, data)
        .then(response => {
          const { data } = response
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // Delete inventory
  deleteInventory(_, id) {
    return new Promise((resolve, reject) => {
      deleteInventory(id)
        .then(response => {
          const { data } = response
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
} 